package com.mvp.shq.washer.ui.shop.shopUserCenter;

import android.os.Bundle;
import android.support.v4.content.ContextCompat;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.jaeger.library.StatusBarUtil;
import com.mvp.shq.washer.R;
import com.mvp.shq.washer.adapter.ShopAddressAdapter;
import com.mvp.shq.washer.base.BaseActivity;
import com.mvp.shq.washer.entity.User;
import com.mvp.shq.washer.entity.shop.UserCountInfo;
import com.mvp.shq.washer.rxbus.Subscribe;
import com.mvp.shq.washer.utils.EventCode;
import com.mvp.shq.washer.utils.JumpUtil;
import com.mvp.shq.washer.utils.SpUtil;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class ShopUserCenterActivity extends BaseActivity<Presenter, Model> implements ShopUserCenterConstract.View {


    @BindView(R.id.back)
    ImageView back;
    @BindView(R.id.title)
    TextView title;
    @BindView(R.id.img_user_head)
    ImageView imgUserHead;
    @BindView(R.id.tv_username)
    TextView tvUsername;
    @BindView(R.id.tv_integral_num)
    TextView tvIntegralNum;
    @BindView(R.id.tv_collect_num)
    TextView tvCollectNum;
    @BindView(R.id.tv_order_num)
    TextView tvOrderNum;
    private User user;

    @Override
    public int getLayoutId() {
        return R.layout.activity_shop_user_center;
    }

    @Override
    public void initView() {
        StatusBarUtil.setColor(this, ContextCompat.getColor(context, R.color.money), 9);
        title.setText("我的");
        setUserInfo();
    }
    @Subscribe(code = EventCode.UPDATE_USER)
    public void setUserInfo(){
        user = SpUtil.getUser();
        if (user == null){
            tvUsername.setText("点击登陆");
        }else {
            tvUsername.setText(user.getNickname());
            tvIntegralNum.setText((int) user.getIntegral()+"");
            imageUtil.loadCircleImage(user.getFaceurl(),imgUserHead,R.mipmap.my_headshot,R.mipmap.my_headshot);
            updateNum();
        }
    }

    @Subscribe(code = EventCode.SHOP_UPDATE_COLLECT_AND_ORDER_NUM)
    public void updateNum(){
        presenter.getUserCountInfo();
    }


    @OnClick({R.id.back, R.id.rl_my_order,R.id.tv_username, R.id.rl_my_address, R.id.rl_my_collect})
    public void onViewClicked(View view) {
        if (view.getId() != R.id.back && user == null){
            JumpUtil.toLogin(context);
            return;
        }
        switch (view.getId()) {
            case R.id.back:
                finish();
                break;
            case R.id.tv_username:
                if (user == null){
                    JumpUtil.toLogin(context);
                }
                break;
            case R.id.rl_my_order:
                JumpUtil.toMyOrder(context);
                break;
            case R.id.rl_my_address:
                JumpUtil.toShopAddress(context, ShopAddressAdapter.TYPE_CHECK);
                break;
            case R.id.rl_my_collect:
                JumpUtil.toShopCollection(context);
                break;
        }
    }

    @Override
    public void getCountInfoS(UserCountInfo info) {
        if (info == null){
            return;
        }
        tvCollectNum.setText(info.getCollectCount()+"");
        tvOrderNum.setText(info.getOrderCount()+"");
    }
}
